home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume6 / tallow < prev    next >
Encoding:
Text File  |  1989-03-21  |  9.9 KB  |  404 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v06i072: tallow --- add entry to ~/.rhosts and later remove it with at(1).
  4. Keywords: More silly code.
  5. Reply-To: tcjones@watdragon.waterloo.edu (speedboat jones)
  6. Distribution: world
  7. Organization: U. of Waterloo, Ontario
  8.  
  9. Posting-number: Volume 6, Issue 72
  10. Submitted-by: tcjones@watdragon.waterloo.edu (speedboat jones)
  11. Archive-name: tallow
  12.  
  13. [In the days following the Great Internet Worm Scare, this is a darned good
  14. idea....  ++bsa]
  15.  
  16. tallow - temporary allow. Put someone in your ~/.rhosts and arrange for them to
  17.          be taken out automatically (using at(1)).
  18.  
  19. Usage: tallow [-c] user host [minutes]
  20.        tallow [-c] host user [minutes]
  21.  
  22. The -c (confirm) option sends you mail when the removal is done.
  23. Minutes is the number of minutes you want the person to be added for.
  24. The default is ten minutes.  The granularity of at(1) (usually 15
  25. minutes) should be kept in mind.
  26.  
  27.  
  28. Terry Jones
  29.  
  30.     Department Of Computer Science,  University Of Waterloo
  31.     Waterloo Ontario Canada N2L 3G1. Phone: 1-519-8884674
  32.     UUCP:                    ...!watmath!watdragon!tcjones
  33.     CSNET, Internet, CDNnet: tcjones@dragon.waterloo.{cdn,edu}
  34.     BITNET:                  tcjones@WATER.bitnet
  35.     Canadian domain:         tcjones@dragon.uwaterloo.ca
  36.  
  37. #! /bin/sh
  38. # This is a shell archive.  Remove anything before this line, then unpack
  39. # it by saving it into a file and typing "sh file".  To overwrite existing
  40. # files, type "sh file -c".  You can also feed this as standard input via
  41. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  42. # will see the following message at the end:
  43. #        "End of archive 1 (of 1)."
  44. # Contents:  tallow tallow/Makefile tallow/README tallow/tallow.c
  45. # Wrapped by tcjones@watdragon on Wed Mar 15 13:38:09 1989
  46. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  47. if test ! -d 'tallow' ; then
  48.     echo shar: Creating directory \"'tallow'\"
  49.     mkdir 'tallow'
  50. fi
  51. if test -f 'tallow/Makefile' -a "${1}" != "-c" ; then 
  52.   echo shar: Will not clobber existing file \"'tallow/Makefile'\"
  53. else
  54. echo shar: Extracting \"'tallow/Makefile'\" \(63 characters\)
  55. sed "s/^X//" >'tallow/Makefile' <<'END_OF_FILE'
  56. CFLAGS = -O
  57. X
  58. tallow: tallow.o
  59. X    cc $(CFLAGS) -o tallow tallow.o
  60. END_OF_FILE
  61. if test 63 -ne `wc -c <'tallow/Makefile'`; then
  62.     echo shar: \"'tallow/Makefile'\" unpacked with wrong size!
  63. fi
  64. # end of 'tallow/Makefile'
  65. fi
  66. if test -f 'tallow/README' -a "${1}" != "-c" ; then 
  67.   echo shar: Will not clobber existing file \"'tallow/README'\"
  68. else
  69. echo shar: Extracting \"'tallow/README'\" \(801 characters\)
  70. sed "s/^X//" >'tallow/README' <<'END_OF_FILE'
  71. tallow - temporary allow. Put someone in your ~/.rhosts and arrange for them to
  72. X         be taken out automatically (using at(1)).
  73. X
  74. Usage: tallow [-c] user host [minutes]
  75. X       tallow [-c] host user [minutes]
  76. X
  77. The -c (confirm) option sends you mail when the removal is done.
  78. Minutes is the number of minutes you want the person to be added for.
  79. The default is ten minutes.  The granularity of at(1) (usually 15
  80. minutes) should be kept in mind.
  81. X
  82. X
  83. Terry Jones
  84. X
  85. X    Department Of Computer Science,  University Of Waterloo
  86. X    Waterloo Ontario Canada N2L 3G1. Phone: 1-519-8884674
  87. X    UUCP:                    ...!watmath!watdragon!tcjones
  88. X    CSNET, Internet, CDNnet: tcjones@dragon.waterloo.{cdn,edu}
  89. X    BITNET:                  tcjones@WATER.bitnet
  90. X    Canadian domain:         tcjones@dragon.uwaterloo.ca
  91. END_OF_FILE
  92. if test 801 -ne `wc -c <'tallow/README'`; then
  93.     echo shar: \"'tallow/README'\" unpacked with wrong size!
  94. fi
  95. # end of 'tallow/README'
  96. fi
  97. if test -f 'tallow/tallow.c' -a "${1}" != "-c" ; then 
  98.   echo shar: Will not clobber existing file \"'tallow/tallow.c'\"
  99. else
  100. echo shar: Extracting \"'tallow/tallow.c'\" \(5422 characters\)
  101. sed "s/^X//" >'tallow/tallow.c' <<'END_OF_FILE'
  102. X/*
  103. X * tallow.c - temporarily add someone to your .rhosts file.
  104. X *
  105. X * usage: tallow [-c] user host [minutes]
  106. X *        tallow [-c] host user [minutes
  107. X *
  108. X *
  109. X * tcjones@watdragon
  110. X *
  111. X */
  112. X
  113. X#include <stdio.h>
  114. X#include <stdlib.h>
  115. X#include <sys/types.h>
  116. X#include <sys/time.h>
  117. X#include <netdb.h>
  118. X#include <pwd.h>
  119. X
  120. X#define DEF_AT_TIME 600            /* seconds */
  121. X#define EDIT "/usr/ucb/edit"
  122. X#define MAIL "/usr/ucb/mail"
  123. X#define ECHO "/bin/echo"
  124. X#define AT "/usr/bin/at -s"
  125. X
  126. main(argc, argv)
  127. int argc;
  128. char **argv;
  129. X{
  130. X    extern char *getenv();
  131. X    extern char *mktemp();
  132. X    extern char *index();
  133. X    extern struct passwd *getpwuid();
  134. X
  135. X    FILE *rfp, *tfp, *fopen(), *fclose();
  136. X
  137. X    char *progname;
  138. X    char *tmp = mktemp("/tmp/tallow.XXXXXXXXX");
  139. X    register char *runtime;
  140. X    register char *host;
  141. X    register char *user;
  142. X    char date[26];
  143. X    char rhosts[40];
  144. X    struct passwd *pwd;
  145. X    register int seconds = DEF_AT_TIME;
  146. X    int    confirm = 0;
  147. X    struct timeval v;
  148. X
  149. X    /* 
  150. X     * This program's name.
  151. X     *
  152. X     */
  153. X    progname = *argv++;
  154. X
  155. X    /*
  156. X     * Find out if we found out who we are etc etc.
  157. X     *
  158. X     */
  159. X    if ((pwd = getpwuid(getuid())) == NULL){
  160. X        fprintf(stderr, "%s: Could not getpwuid\n", progname);
  161. X    }
  162. X
  163. X
  164. X    /*
  165. X     * Open the temporary file that we will write the at(1) commands to
  166. X     * for the removal of the .rhosts entry.
  167. X     *
  168. X     */
  169. X    if ((tfp = fopen(tmp, "w")) == NULL) {
  170. X        printf("Could not open %s, .rhosts untouched\n", tmp);
  171. X        diy();
  172. X        exit(1);
  173. X    }
  174. X
  175. X
  176. X    /*
  177. X     * If the -c option is present, record that and move on through the
  178. X     * command line options as normal.
  179. X     *
  180. X     */
  181. X    if (argc > 1 && !strcmp(*argv, "-c")) {
  182. X        confirm = 1;
  183. X        argc--;
  184. X        argv++;
  185. X    }
  186. X
  187. X
  188. X    /* 
  189. X     * If there are 3 arguments only (e.g. tallow fred watmath) then
  190. X     * no time has been given so use the default. Otherwise if there
  191. X     * are not exactly 4 arguments (e.g. tallow fred watmath 30) we
  192. X     * have an error. 
  193. X     *
  194. X     */
  195. X    if (argc != 4 && argc != 3) usage(progname);
  196. X
  197. X
  198. X    /*
  199. X     * The host name and the user name can appear in either order. If
  200. X     * we can't recognise a hostname in either of them then get out.
  201. X     * Set up the host and user pointers appropriately...
  202. X     *
  203. X     */
  204. X    if (gethostbyname(*argv) == NULL) {
  205. X        if (gethostbyname(*(argv+1)) == NULL) {
  206. X            printf("%s: neither %s nor %s are valid machine names.\n",
  207. X                *argv, *(argv+1));
  208. X            usage(progname);
  209. X        }
  210. X        else {
  211. X            user = *argv;
  212. X            host = *(argv+1);
  213. X        }
  214. X    }
  215. X    else {
  216. X        host = *argv;
  217. X        user = *(argv+1);
  218. X    }
  219. X
  220. X    /*
  221. X     * Skip over the host and user names.
  222. X     *
  223. X     */
  224. X    argv+=2;
  225. X
  226. X
  227. X    /*
  228. X     * Calculate the number of seconds required until the .rhosts entry
  229. X     * should be removed. This is either the default or 60 * whatever 
  230. X     * they entered.
  231. X     *
  232. X     */
  233. X    seconds = (*argv) ?  60*(atoi(*argv)) : DEF_AT_TIME;
  234. X
  235. X
  236. X    /*
  237. X     * Make sure everything looks ok.
  238. X     *
  239. X     */
  240. X    if (seconds <= 0) {
  241. X        printf("%s: Invalid time %d\n", progname, seconds);
  242. X        exit(1);
  243. X    }
  244. X
  245. X
  246. X    /*
  247. X     * Try to open their .rhosts file.
  248. X     *
  249. X     */
  250. X    sprintf(rhosts, "/u/%s/.rhosts", pwd->pw_name);
  251. X    if ((rfp = fopen(rhosts, "a")) == NULL) {
  252. X        printf("%s: Could not open %s\n", progname, rhosts);
  253. X        exit(1);
  254. X    }
  255. X
  256. X    /*
  257. X     * Add the new entry at the end and close the file.
  258. X     *
  259. X     */
  260. X    fprintf(rfp, "%s %s\n", host, user);
  261. X    fclose(rfp);
  262. X
  263. X
  264. X    /*
  265. X     * Start to produce the file for at. The first thing it must do is
  266. X     * edit the .rhosts and remove the line. Use a here document.
  267. X     *
  268. X     */
  269. X    fprintf(tfp, "%s>/dev/null %s<<*\ng/^%s %s$/d\nw\n*\n",
  270. X        EDIT, rhosts, host, user);
  271. X
  272. X
  273. X    /*
  274. X     * If they want confirmation, arrange to send them some mail
  275. X     * when it is done. Could send them the .rhosts file too.
  276. X     *
  277. X     */
  278. X    if (confirm) {
  279. X        fprintf(tfp,"%s \\\"%s %s\\\" removed from %s|%s -s %s %s\n",
  280. X            ECHO,host,user,rhosts,MAIL,progname,pwd->pw_name);
  281. X    }
  282. X
  283. X
  284. X    /*
  285. X     * Close the file for at.
  286. X     *
  287. X     */
  288. X    fclose(tfp);
  289. X
  290. X    /*
  291. X     * Get the time of day so we can work out when to schedule at for.
  292. X     *
  293. X     */
  294. X    if (gettimeofday(&v, NULL) == -1){
  295. X        fprintf(stderr, "%s: Could not get time of day.\n", progname);
  296. X        diy();
  297. X        exit(1);
  298. X    }
  299. X
  300. X    /* 
  301. X     * Add on the required number of seconds and convert the time to a string
  302. X     * like "Sun Sep 16 01:03:52 1973\n\0"
  303. X     *
  304. X     */
  305. X    v.tv_sec += seconds;
  306. X    strcpy(date, ctime((time_t *)(&(v.tv_sec))));
  307. X
  308. X
  309. X    /*
  310. X     * Find the first colon in the above, set the next colon (3 chars on)
  311. X     * to '\0', and move back a couple of charaters to the start of the 
  312. X     * time. Thus with the above string we'd end up with
  313. X     *
  314. X     * "Sun Sep 16 01:03'\0'52 1973\n\0"
  315. X     *             ^
  316. X     *             |
  317. X     *     runtime--
  318. X     *
  319. X     * With runtime pointing at the null-terminated time.
  320. X     *
  321. X     */
  322. X    if ((runtime = index(date, ':')) == NULL){
  323. X        fprintf(stderr, "%s: Could not get the time correctly,\n", progname);
  324. X        diy();
  325. X        exit(1);
  326. X    }
  327. X
  328. X    *(runtime+3) = '\0';
  329. X    runtime -= 2;
  330. X    
  331. X
  332. X
  333. X    /*
  334. X     * System an at command to run the file at the right time.
  335. X     * You need to use at -s since we are using a here document and
  336. X     * csh (and its friends) won't like that.
  337. X     *
  338. X     */
  339. X    {char cmd[100];  /* Can't possibly be this long. */
  340. X        sprintf(cmd, "%s %s %s", AT, runtime, tmp);
  341. X        if (system(cmd)) {
  342. X            printf("%s: %s fails!\n", progname, cmd);
  343. X            diy();
  344. X        }
  345. X    }
  346. X
  347. X
  348. X    /*
  349. X     * Get rid of the temporary.
  350. X     *
  351. X     */
  352. X    if (unlink(tmp) == -1) {
  353. X        printf("%s: Warning, could not remove %s\n", progname, tmp);
  354. X        exit(1);
  355. X    }
  356. X
  357. X    return 0;
  358. X}
  359. X
  360. diy()
  361. X{
  362. X    /*
  363. X     * Complain.
  364. X     *
  365. X     */
  366. X    fprintf(stderr, "Cannot arrange for automatic .rhosts removal!\n");
  367. X    fprintf(stderr, "Do it yourself.\n");
  368. X}
  369. X
  370. usage(me)
  371. char *me;
  372. X{
  373. X    /*
  374. X     * Complain bitterly.
  375. X     *
  376. X     */
  377. X    fprintf(stderr, "Usage: %s [-c] host user [minutes]\n", me);
  378. X    exit(1);
  379. X}
  380. END_OF_FILE
  381. if test 5422 -ne `wc -c <'tallow/tallow.c'`; then
  382.     echo shar: \"'tallow/tallow.c'\" unpacked with wrong size!
  383. fi
  384. # end of 'tallow/tallow.c'
  385. fi
  386. echo shar: End of archive 1 \(of 1\).
  387. cp /dev/null ark1isdone
  388. MISSING=""
  389. for I in 1 ; do
  390.     if test ! -f ark${I}isdone ; then
  391.     MISSING="${MISSING} ${I}"
  392.     fi
  393. done
  394. if test "${MISSING}" = "" ; then
  395.     echo You have the archive.
  396.     rm -f ark[1-9]isdone
  397. else
  398.     echo You still need to unpack the following archives:
  399.     echo "        " ${MISSING}
  400. fi
  401. ##  End of shell archive.
  402. exit 0
  403.  
  404.